from django.shortcuts import HttpResponse, render
from django.urls import reverse


# 在URL中携带参数
# 1.通过查询字符串(query string) key=value的形式
# 2. 在path中携带 /book/2

# 1.查询字符串 http://127.0.0.1:8000/book?id=3&type=1
def book_detail_query_string(request):
    book_id = request.GET.get('id')
    book_type = request.GET.get('type')
    return HttpResponse(f"您查找的图书id是{book_id},图书类型是{book_type}")


def book_detail_path(request, book_id, book_type):
    # 如果urls 设置了命名空间，name前面则需要带上这个值,如这里的book
    print(reverse("book:book_detail_path", kwargs={"book_id": book_id, "book_type": book_type}))
    return HttpResponse(f"path方式您查找的图书id是{book_id},图书类型是{book_type}")


def index(request):
    return render(request, "index.html")


def home_index(request):
    return render(request, "homeIndex.html")


def info(request):
    # 1.传递普通变量
    username = "海晨忆"
    # 2.字典类型
    book_info = {'name': '西游记', 'author': '吴承恩'}
    # 3.列表
    book_infos = [
        {'name': '红楼梦', 'author': '曹雪芹'},
        {'name': '水浒传', 'author': '施耐庵'},
    ]

    # 4.对象
    class Person:
        def __init__(self, name: str, age: int):
            self.name = name
            self.age = age

    context = {
        'username': username,
        'bookInfo': book_info,
        'bookInfos': book_infos,
        'person': Person('张三', 18),
    }
    return render(request, "info.html", context=context)
